﻿Public Class frmProjectsInsert

    Dim strNameForm As String = "Insert Project"
    Public mId As Integer

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
        Try
            If IsComplete() Then
                Dim oTProjects As New Data.TProjects
                oTProjects.mName = txtName.Text
                oTProjects.mDescription = txtDescription.Text
                oTProjects.mStartDate = txtDate.DataMySql

                mId = oTProjects.Insert
                If mId > 0 Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                Else
                    MsgBox("Impossible insert the project.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
                oTProjects = Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "cmdInsert_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtName.Text = "" Then
                MsgBox("Insert name.", vbExclamation, Me.Name)
                txtName.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class